﻿@model List<Himall.Model.BusinessCategoryInfo>
@{
    ViewBag.Title = "BusinessCategory";
}
<script src="~/Scripts/jquery.himallLinkage.nocreate.js"></script>
<script src="~/Scripts/CommonJS.js"></script>
<script type="text/javascript">
    var categoryArray = new Array();
    $(function () {

        var _order;
        $('.container').on('focus', '.text-order', function () {
            _order = parseFloat($(this).val());
        });

        $('.container').on('blur', '.text-order', function () {
            if ($(this).hasClass('text-order')) {
                if (isNaN($(this).val()) || parseInt($(this).val()) <= -1 || parseInt($(this).val()) >=100) {
                    $.dialog({
                        title: '更新分类信息',
                        lock: true,
                        width: '400px',
                        padding: '20px',
                        content: ['<div class="dialog-form">您输入的分佣比例不合法,此项只能是大于0且小于100的数字.</div>'].join(''),
                        button: [
                        {
                            name: '关闭',
                        }]
                    });
                    $(this).val(_order);
                } else {
                    if (parseFloat($(this).val()) === _order) return;
                    var id = $(this).parents("tr.bcategoryLines").attr('id');
                    var commisRate = $(this).val();
                    var businessCategroyId = $(this).parents("tr.bcategoryLines").attr('businessCategoryId');
                    categoryArray.remove(id + '|' + _order);
                    categoryArray.push(id + '|' + commisRate);
                    //$.post('UpdateShopCommisRate', { businessCategoryId: businessCategroyId, commisRate: commisRate }, function (result) {
                    //    if (result.success)
                    //        $.dialog.tips('保存成功');
                    //    else
                    //        $.dialog.errorTips('保存失败！' + result.msg);
                    //})


                }
            }
        });

        (function () {
            $(".bcategoryLines").each(function () {
                var id = $(this).attr('id');
                var commisRate = $(this).find('.commisRate').val();
                categoryArray.push(id + '|' + parseFloat(commisRate));
            });

        })();

        var categoryId;
        var categoryName = new Array();
        $('.add-business').click(function () {
            $.dialog({
                title: '新增经营类目',
                lock: true,
                id: 'addBusiness',
                content: ['<div class="dialog-form">',
				'<div class="form-group">',
					'<label class="label-inline" for="">经营类目</label>',
					'<select id="category1" class="form-control input-sm select-sort"><option></option></select>',
					'<select id="category2" class="form-control input-sm select-sort"><option></option></select>',
					'<select id="category3" class="form-control input-sm select-sort"><option></option></select>',
				'</div>',
				'<div class="form-group">',
					'<label class="label-inline" for="">分佣比例</label>',
					'<input class="form-control input-sm input-num" type="text" id="CommisRate"> %',
				'</div>',
			'</div>'].join(''),
                padding: '20px 10px',
                okVal: '确认',
                ok: function () {
                    var reg = /^[-+]?(0|[1-9]\d*)(\.\d+)?$/;
                    if (categoryName.length < 3)
                    {
                        $.dialog.errorTips("请选择完整后再试！");
                        return false;
                    }
                    if (!$("#CommisRate").val())
                    {
                        $.dialog.errorTips("请填写分佣比例！");
                        return false;
                    }
                    if (reg.test($("#CommisRate").val()) == false)
                    {
                        $.dialog.errorTips("请填写正确的分佣比例");
                        return false;
                    }
                    if ($("#" + categoryId).length > 0) {
                        $.dialog.errorTips("新建失败,该类目已经存在！");
                        return false;
                    }
                    var html = ['<tr class="bcategoryLines" id="' + categoryId + '" businessCategoryId="'+4+'">',
                            '<td>' + categoryName.join(' > ') + '</td>',
                            '<td><input class="text-order no-m commisRate" type="text" value="' + $("#CommisRate").val() + '"> %</td>',
                            '<td class="td-operate"><span class="btn-a"><a onclick="deleteCategoryLine(this);" class="a-del">删除</a></span></td>',
                        '</tr>'];
                    $("#bcategoryTBody").append($(html.join('')));
                    $("#nonData").remove();

                    categoryArray.push(categoryId + '|' + $("#CommisRate").val());

                    categoryId = 0;
                    categoryName.length = 0;
                }
            });
            $('#category1,#category2,#category3').himallLinkage({
                url: '../category/GetValidCategories',
                enableDefaultItem: true,
                defaultItemsText: '请选择',
                onChange: function (level, value, text) {
                    categoryId = value;
                    if (value) {
                        var categoryNames = [];
                        for (var i = 0; i < level; i++)
                            categoryNames.push(categoryName[i]);
                        categoryNames.push(text);
                        categoryName = categoryNames;
                    }
                    if (level == 2) {
                        var loading = showLoading();
                        ajaxRequest({
                            type: 'GET',
                            url: "./GetCategoryCommisRate",
                            param: { Id: value },
                            dataType: "json",
                            success: function (data) {
                                loading.close();
                                if (data.successful == true) {
                                    $("#CommisRate").val(data.CommisRate);
                                }
                            }, error: function () {
                                loading.close();
                            }
                        });
                    }
                    //alert(text);
                }
            });
        });


        $("#SaveBtn").click(function () {
            var loading = showLoading();
            ajaxRequest({
                type: 'POST',
                url: "./SaveBusinessCategory",
                param: { shopId: $("#shopId").val(), bcategory: categoryArray.join(',') },
                dataType: "json",
                success: function (data) {
                    if (data.Successful == true) {
                        location.href = "./Management";
                    }
                    loading.close();
                }, error: function () { loading.close(); }
            });
        });

    });

    function deleteCategoryLine(obj) {
        var id = $(obj).parents("tr.bcategoryLines").attr('id');
        var commisRate = $(obj).parents("td").prev().find('.commisRate').val();
        //console.log(id + '|' + parseInt( commisRate));
        categoryArray.remove(id + '|' + parseInt(commisRate));
        $("tr#" + id).remove();
        //console.log(categoryArray);
    }


</script>
<div class="container">
    <ul class="nav nav-tabs-custom clearfix">
        <strong>店铺</strong>
        <li><a href="./Management">管理</a></li>
        <li><a href="./Management?type=Auditing">开店审核</a></li>
        <li class="active"><a>经营类目</a></li>
        <em class="primary-btn">
            <span class="glyphicon glyphicon-question-sign"></span>
            <div class="primary">
                <h3>温馨提示：</h3>
                <span>*</span><p>管理店铺的经营类目</p>
            </div>
        </em>
    </ul>

    <div class="form-group">
        <table class="table table-bordered">
            <thead>
                <tr>
                    <th>经营类目</th>
                    <th>分佣比例</th>
                    <th class="td-operate">操作</th>
                </tr>
            </thead>
            <tbody id="bcategoryTBody">
                @if (Model == null || Model.Count() == 0)
                {
                    <tr id="nonData"><td colspan="3" style="text-align:center;"><h4>没有任何经营类目</h4></td></tr>
                }
                else
                {
                    foreach (var item in Model)
                    {
                        <tr class="bcategoryLines" id="@item.CategoryId" businessCategoryId="@item.Id">
                            <td>@item.CategoryName</td>
                            <td><input class="text-order no-m commisRate" type="text" value="@item.CommisRate"> %</td>
                            <td class="td-operate"><span class="btn-a"><a onclick="deleteCategoryLine(this);" class="a-del">删除</a></span></td>
                        </tr>
                    }
                }
            </tbody>
        </table>
        <a class="add-business"><span class="glyphicon glyphicon-plus-sign"></span>新增经营类目</a>
    </div>
    <input type="button" class="btn btn-primary" id="SaveBtn" value="保存" />

</div>

<input type="hidden" id="shopId" value="@ViewBag.ShopId" />